<?php
/**
 * 科室信息管理控制器
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/23
 * Time: 20:27
 */

namespace Admin\Controller;


use Admin\Model\BaseinfoVersionModel;
use Admin\Model\DepartmentinfoModel;
use Base\Controller\CurdController;
use Library\Utils;

class DepartmentinfoController extends CurdController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
            $where['status']=1;
            $list=$this->model->where($where)->select();
            $this->assign("list",$list);
        }elseif($_REQUEST['pid']){
            $where['pid']=$_REQUEST['pid'];
            $list=$this->model->where($where)->select();
            $this->assign("list",$list);
        }elseif ($_REQUEST['del']){
            $where['status']=0;
            $list=$this->model->where($where)->select();
            $this->assign("list",$list);
        }else{
            $list =$this->getdepartmentList();
            $lists = \Library\Utils::toTrees($list,'pid');
            $this->assign("list",$lists);
        }
    }
    /**
     * 新增
     */
    public function add()
    {
        $data = $this->model->create();
        if (!$data) {
            $this->error($this->model->getError());
        } else {

            $data = $this->addAfter($data);
            $columnNameArr = explode(',', $data['name']);
            $i = 0;
            foreach ($columnNameArr as $k => $v) {
                if (!$v) continue;
                $data['name'] = $v;
                $act = $this->model->add($data);
                if ($act) $i++;
            }
            $this->success('操作成功，添加' . $i . '个栏目', $this->urlConfig['add_success']);
        }
    }
    protected function addAfter($data)
    {
        if ($data['pid'] > 0) $data['level'] = $this->model->where('id=' . $data['pid'])->getField('level') + 1;
        $ids=$this->model->order('id desc')->find();
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        return $data;
    }

    protected function editAfter($data)
    {
        if ($data['pid'] > 0) $data['level'] = $this->model->where('id=' . $data['pid'])->getField('level') + 1;
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        return $data;
    }
    public function  getmoreList(){
         $list=$this->model->relation(true)->where(array('pid'=>0))->select();
         $this->ajaxReturn($list);
    }
}